package com.example.demo.mapper;

import java.util.List;


import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import com.example.demo.entity.Food;
import com.example.demo.entity.Order_Details;
import com.example.demo.entity.Order_Main;


@Repository
public interface Order_Main_Mapper extends Basic_Mapper<Order_Main> {

    @Select("select food.id,food.name,sum(Order_Details.count) price "
            + " from Order_Details inner join food on food.id=Order_Details.food_id "
            + " where food.foodtype_id=#{foodtype_id} and Order_Details.status=#{status} "
            + " group by food.id ")
    public List<Food> getFoodByType(Food f);


    @Select("select Order_Details.*, food.name food_name,Order_Main.code,Order_Main.date,desk.name desk_name from"
            + " Order_Details inner join food on food.id=Order_Details.food_id "
            + " inner join Order_Main on Order_Main.id=Order_Details.order_id "
            + " inner join desk on desk.id=Order_Main.desk_id "
            + " where Order_Details.food_id=#{food_id} and Order_Details.status=#{status}")

    public List<Order_Details> getFoodBystatus(Order_Details d);

    @Select("select Order_Main.*,desk.name desk_name from Order_Main inner join desk"
            + " on desk.id=Order_Main.desk_id ${where} ${limit}")
    //@Select("select Order_Main.*,desk.name,desk_name from Order_Main ${where} ${limit}")
    public List<Order_Main> select(@Param("where") String where, @Param("limit") String limit);


    @Select("select Order_Details.*,food.name food_name  from Order_Details inner join food"
            + " on food.id=Order_Details.food_id where Order_Details.order_id=#{order_id}")
    //@Select("select Order_Main.*,desk.name,desk_name from Order_Main ${where} ${limit}")
    public List<Order_Details> selectdetails(int order_id);

    @Delete("delete from Order_Main where id=#{id}")
    public void delete(int id);

    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    @Insert("insert into Order_Main(code,date,desk_id,amount,reamount,status,paytype)"
            + " values(#{code},#{date},#{desk_id},#{amount},#{reamount},#{status},#{paytype})")
    public void insert(Order_Main t);

    @Insert("insert into Order_details(order_id,food_id,count,price,status,changer_user_id,changer_date,amount)"
            + " values(#{order_id},#{food_id},#{count},#{price},#{status},#{changer_user_id},#{changer_date},#{amount})")
    public void insertdetails(Order_Details t);

    @Update("update Order_Details set status=#{status},changer_user_id=#{changer_user_id},changer_date=#{changer_date} where id=#{id}")
    public void updatedetails(Order_Details t);


    @Update("update Order_Details set status=#{status},changer_user_id=#{changer_user_id},changer_date=#{changer_date} where order_id=#{order_id}")
    public void updatedetailsByOrder(Order_Details t);

    @Select("select * from Order_Main where id=#{id}")
    public Order_Main getById(int id);

    @Select("select * from Order_Details where id=#{id}")
    public Order_Details getDetailsById(int id);


    @Update("update Order_Main set status=#{status},amount=#{amount},reamount=#{reamount} where id=#{id}")
    public void update(Order_Main t);
}
